package com.ssm.controller;

import com.ssm.pojo.AirTicket;
import com.ssm.pojo.OrderForm;
import com.ssm.pojo.Ticket;
import com.ssm.pojo.User;
import com.ssm.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TicketController {

    @Autowired
    private TicketService ticketService;

    @RequestMapping("addTicket.html")
    public String addTicket(Ticket ticket ,Integer id, HttpSession session){
        //用户ID
        User user = (User) session.getAttribute("SESSION_USER");
        ticket.setUser(user);
        //机票ID
        AirTicket airTicket = new AirTicket();
        airTicket.setId(id);
        ticket.setAirTicket(airTicket);
        //订单号
        ticket.setOrderNumber("123"+System.currentTimeMillis()+"456");
        //订单状态
        ticket.setStatus(1);
        //订单状态
        ticket.setSettleAccounts(1);
        //创建订单时间
        Date dt =new Date();
        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
        String formatDate = dFormat.format(dt);
        ticket.setOrderTime(formatDate);

        ticketService.addTicket(ticket);
        return "/leading/ticketCartSuccess";
    }


    @RequestMapping("getTicket.html")
    public String getTicket(Model model){
        List<Ticket> tickets = ticketService.getTicketFormBy(1);
        model.addAttribute("tickets",tickets);
        return "leading/ticketCar";
    }

    @RequestMapping("delectTicket.html")
    public String delectOrderForm(Integer id){
        ticketService.delectTicketForm(id);
        return "forward:getTicket.html";
    }

    @RequestMapping("updateTicket.html")
    public String updateTicket(Ticket ticket){
        ticket.setSettleAccounts(2);
        ticketService.updateTicketForm(ticket);
        return "leading/success";
    }

    @RequestMapping("getTickets.html")
    public String getTickets(Model model,HttpSession session){
        User user = (User) session.getAttribute("SESSION_USER");

        List<Ticket> ticketList = ticketService.getTicket(user.getId());
        model.addAttribute("ticketList",ticketList);
        return "user/vipTicket";
    }
}
